<style scoped>
.form-list-data-table {
  margin: 12px 0;
}
.form-list-data-table /deep/ .el-table th {
  color: #555;
}
</style>
<!--  -->
<template>
  <div class="form-list-data-table">
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="表单名称">
      </el-table-column>
      <el-table-column prop="total" label="提交数据总数" align="center">
      </el-table-column>
      <el-table-column prop="quote" label="页面引用" align="center">
      </el-table-column>
      <el-table-column prop="createAt" label="创建时间" width="180">
        <template slot-scope="scope">
          {{scope.row.createAt | dateformat('YYYY-MM-DD')}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="380">
        <template slot-scope="scope">
          <el-button @click="goFormInfo(scope.row.id)" type="text " size="small">进入</el-button>
          <el-button @click="editorForm(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteForm(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑表单 -->
    <el-dialog
      title="编辑表单"
      :visible.sync="dialogFormVisible"
      width="30%"
      @closed="closed"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        v-loading="labelLoading"
        :rules="rules"
        ref="labelSave"
        label-width="80px"
        @submit.native.prevent
      >
        <el-form-item
          label="表单名称"
          prop="formName"
        >
          <el-input
            v-model.trim="form.formName"
            autocomplete="off"
            size="medium"
          ></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button size="small" @click="closed">取 消</el-button>
        <el-button size="small" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getFormListPathByItem } from '@/core/functions.js'
export default {
  props: {
    dataTotal: {
      type: Function
    }
  },
  data () {
    var checkIshas = (rule, value, callback) => {
      let bool = true
      this.tableData.map(item => {
        if (item.id !== this.form.id) {
          if (value === item.name) {
            bool = false
          }
        }
      })
      if (!bool) {
        callback(new Error('表单名称已存在'))
      } else {
        return callback()
      }
    }
    return {
      loading: true,
      // 表格数据
      tableData: [],
      // 请求返回的数据
      data: [],
      dialogFormVisible: false,
      labelLoading: false,
      form: {},
      rules: {
        formName: [
          { required: true, message: '请输入表单名称', trigger: 'blur' },
          { min: 1, max: 24, message: '表单名称长度不能超过24个字！', trigger: 'blur' },
          { validator: checkIshas, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    initData () {
      this.loading = true
      this.$post('/form/page-form-list').then((response) => {
        this.loading = false
        if (response && response.length > 0) {
          this.data = response
          this.tableData = makeInitData(response)
        } else {
          this.data = []
          this.tableData = []
        }
      })
    },
    getTableData () {
      return this.tableData
    },
    goFormInfo (formId) {
      let item = this.data.find(element => {
        return parseInt(element.id) === parseInt(formId)
      })
      let fullPath = getFormListPathByItem(item)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    editorForm (formId) {
      let item = this.data.find(element => {
        return parseInt(element.id) === parseInt(formId)
      })
      if (!item) {
        return
      }
      this.form = JSON.parse(JSON.stringify(item))
      this.dialogFormVisible = true
      // this.$prompt('修改表单名称！', '修改', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   inputValue: item.formName,
      //   closeOnClickModal: false,
      //   inputValidator: (str) => {
      //     if (str && str.trim().length > 24) {
      //       return '表单名称长度不能超过24个字！'
      //     }
      //     if (!str || str.trim().length === 0) {
      //       return '表单名称不能为空！'
      //     }
      //     return true
      //   },
      //   inputErrorMessage: '表单名称不真确'
      // }).then(({ value }) => {
      //   // 请求后台添加
      //   this.loading = true
      //   this.$post('/form/page-form-update', {
      //     id: item.id,
      //     name: value
      //   }).then(response => {
      //     this.loading = false
      //     this.initData()
      //   })
      // }).catch(() => { })
    },
    submit () {
      this.$refs['labelSave'].validate((valid) => {
        if (valid) {
          this.labelLoading = true
          this.$post('/form/page-form-update', {
            id: this.form.id,
            name: this.form.formName
          }).then(response => {
            this.initData()
            this.labelLoading = false
            this.dialogFormVisible = false
          })
        } else {
          return false
        }
      })
    },
    closed () {
      this.labelLoading = false
      this.form = {}
      if (this.$refs['labelSave']) {
        this.$refs['labelSave'].clearValidate()
      }
      this.dialogFormVisible = false
    },
    deleteForm (formId) {
      let item = this.data.find(element => {
        return parseInt(element.id) === parseInt(formId)
      })
      if (item.quote > 0) {
        this.$alert('先删除页面中表单的引用再删除表单！')
        return
      }
      // 删除
      this.$confirm('删除后前台提交的表单的数据均会清除，确定?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$post('/form/page-form-delete', {
          formId: formId
        }).then(response => {
          this.loading = false
          this.initData()
        })
      }).catch(() => { })
    },
    resetData () {
      this.initData()
    }
  }
}

function makeInitData (data) {
  return data.map(element => {
    return {
      id: element.id,
      name: element.formName,
      total: element.total,
      quote: element.quote,
      createAt: element.createAt
    }
  })
}

</script>
